#include "stdafx.h"
#include "FtpConnection.h"
#include "ConfigMgr.h"
#include "FtpMgr.h"

CFtpMgr::CFtpMgr() : m_pFtpConnection(NULL)
{
	m_pFtpConnection = CFtpConnection::CreateInstance();
	if (NULL != m_pFtpConnection)
	{
		printf("create network session...\n");
		m_pFtpConnection->FtpSession();
	}
}

CFtpMgr::~CFtpMgr()
{
	if (m_pFtpConnection != NULL)
	{
		m_pFtpConnection->FtpClose();
		m_pFtpConnection->DestroyInstance();
		m_pFtpConnection = NULL;
	}
}

BOOL CFtpMgr::ConnectFtp()
{
	::ZeroMemory(&m_ftpdata, sizeof(FTPDATA));
	BOOL bRet = FALSE;
	bRet = m_pConfigMgr->LoadFtpConfig(&m_ftpdata);
	if (!bRet)
	{
		return bRet;
	}
	printf("connect ftp\n");
	return m_pFtpConnection->FtpConnect(&m_ftpdata);
}

BOOL CFtpMgr::DownloadFiles()
{
	::ZeroMemory(&m_downloadData, sizeof(DOWNLOADDATA));
	BOOL bRet = FALSE;
	bRet = m_pConfigMgr->LoadDownloadConfig(&m_downloadData);
	if (!bRet)
	{
		return bRet;
	}
	printf("begin download...\n");
	m_pFtpConnection->FtpDownloadDirEx(&m_downloadData);
	return TRUE;
}

BOOL CFtpMgr::GetFileList(const CString & strPath, vector<CString>& vFileList)
{
	return m_pFtpConnection->FtpGetFileList(strPath, vFileList);	
}

BOOL CFtpMgr::UploadFile(const CString & strRemotePath, const CString & strFilePath)
{
	return m_pFtpConnection->FtpUploadFile(strRemotePath, strFilePath);
}